package cn.t.response;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Date;

@Data
@AllArgsConstructor
public class Response<T> {
    private String code;
    private String msg;
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
    private Date timestamp;
    private T data;

    public Response(){
        this.timestamp=new Date();
    }

    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setCode("200");
        response.setMsg("success");
        response.setTimestamp(new Date());
        response.setData(data);
        return response;
    }
    /**
     * 失败
     */
    public static <T> Response<T> error(String code, String message) {
        Response<T> response = new Response<T>();
        response.setCode(code);
        response.setMsg(message);
        response.setTimestamp(new Date());
        return response;
    }

    public static <T> Response<T> error(String code, String message, T data) {
        Response<T> response = new Response<T>();
        response.setCode(code);
        response.setMsg(message);
        response.setTimestamp(new Date());
        response.setData(data);
        return response;
    }
}
